//-----------------------------------------------------------------------
// <copyright file="Checkin.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Giles Knap [Updated 03 Apr 08: Mike Fourie - Code Cleanup, TFS 2008 Support, Workingdirectory Support etc.]</author>
// <date>2006-03-23</date>
// <summary>Creates a SourceTfs branch</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.SourceTfs
{
    using System;
    using System.Globalization;
    using Microsoft.Build.Framework;
    using Microsoft.Sdc.Tasks.Configuration;

    /// <summary>
    /// Checks in the speicied file or folder
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[
    /// <SourceTfs.Checkin Path="Path" Comments="Comments" TfsVersion="tfsVersion" WorkingDirectory="workingDirectory" Override="overrideText"/>]]></code>
    /// <para>where:</para>
    /// <para><i>Path (Required)</i></para>
    /// <para>File Spec of file or folder to checkin.</para>
    /// <para><i>Comments (Required)</i></para>
    /// <para>Comments to go into the changeset</para>
    /// <para><i>tfsVersion</i></para>
    /// <para>Version of TFS to use. Supports 2005 and 2008. Default is 2005</para>
    /// <para><i>workingDirectory</i></para>
    /// <para>Specifies the WorkingDirectory to run TF.exe in. This determines the workspace</para>
    /// <para><i>overrideText</i></para>
    /// <para>Text to use when overriding checkin policies. Using override may cause a warning.</para>   
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <SourceTfs.Checkin
    ///             Path="$/Depot/Main/Working/test.txt" 
    ///             Comments="Auto update of test.txt"
    ///         />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class Checkin : TaskBase
    {
        private string comments;
        private string path;
        private string tfsVersion = "2005";
        private string overrideText;
        private string workingDirectory;

        /// <summary>
        /// Gets or sets the working directory.
        /// </summary>
        /// <value>The working directory.</value>
        public string WorkingDirectory
        {
            get { return this.workingDirectory; }
            set { this.workingDirectory = value; }
        }

        /// <summary>
        /// Gets or sets the TFS version.
        /// </summary>
        /// <value>The TFS version.</value>
        public string TfsVersion
        {
            get { return this.tfsVersion; }
            set { this.tfsVersion = value; }
        }

        /// <summary>
        /// Gets or sets the override.
        /// </summary>
        /// <value>The override.</value>
        public string Override
        {
            get { return this.overrideText; }
            set { this.overrideText = value; }
        }

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        /// <value>The comments.</value>
        [Required]
        public string Comments
        {
            get { return this.comments; }
            set { this.comments = value; }
        }

        /// <summary>
        /// Gets or sets the folder or file to be checked in
        /// </summary>
        /// <value>FilesSpec</value>
        [Required]
        public string Path
        {
            get { return this.path; }
            set { this.path = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            Helper h = new Helper(this.TfsVersion);
            string arguments = String.Format(CultureInfo.InvariantCulture, "checkin \"{0}\" /comment:\"{1}\" /recursive /noprompt", this.path, this.comments);
            if (string.IsNullOrEmpty(this.Override) == false)
            {
                arguments += " /override:\"" + this.Override + "\"";
            }
            
            ShellExecute shellExecute = new ShellExecute(h.TfExePath, arguments);
            if (string.IsNullOrEmpty(this.WorkingDirectory) == false)
            {
                shellExecute.WorkingDirectory = this.WorkingDirectory;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format("Executing {0} with {1}", shellExecute.Filename, arguments));
            int returnValue = shellExecute.Execute();
            Log.LogMessage(MessageImportance.Low, shellExecute.StandardOutput);
            switch (returnValue)
            {
                case 1:
                    Log.LogWarning("Exit Code 1. Partial success: " + shellExecute.StandardError.Trim());
                    break;
                case 2:
                    Log.LogError("Exit Code 2. Unrecognized command: " + shellExecute.StandardError.Trim());
                    break;
                case 100:
                    Log.LogError("Exit Code 100. Nothing Succeeded: " + shellExecute.StandardError.Trim());
                    break;
            }
        }
    }
}